﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using web.core.ViewModels;
using web.core.Models;
using web.component; 

namespace web.core.Controllers
{
    public class admClientTestimonialController : baseController
    {
        //------------------------------------
        //ct_list PAGE
        //------------------------------------
        #region ct_list PAGE
        public ActionResult ct_list()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ctr_ct_list(int? pageindex)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            return ctr_ct_list(pageindex, admin_const.PAGE_LIST_SIZE, languageId);
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult ctr_ct_list(int? pageindex, int? pagesize, int? languageId)
        {
            var lstdelete = Request.Form["is_delete"];
            if (!string.IsNullOrEmpty(lstdelete)) {
                var arr = lstdelete.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach(var iterator in arr){
                    if (iterator != "false")
                        clientTestimonialsModels.deletePage(Convert.ToInt16(iterator));
                }
            } 
            var page_view = new ViewModels.ClientTestimonialPageListViewModels()
            { 
                //list_group = accountModels.getListUserGroup(),
                list_pagesize = GetSizePagingPublic(pagesize ?? admin_const.PAGE_LIST_SIZE),
                pagesize = pagesize ?? admin_const.PAGE_LIST_SIZE
            };
            TryUpdateModel(page_view);
            if (page_view.languageId != null)
            {
                languageId = page_view.languageId;
            }
            var langid = (languageId ?? admin_const.DEFAULT_LANGUAGE_ID);

            var lst_lang = languageModels.get_list_language();
            page_view.select_list_language = new SelectList(lst_lang, "language_id", "language_desc");

            page_view.page_list_ctestimonial = Models.clientTestimonialsModels.adm_client_testimonial_list(pageindex ?? 1, pagesize ?? admin_const.PAGE_LIST_SIZE, langid);
            return PartialView("../Shared/client_testimonial/list_ct", page_view);
        }

        public ActionResult updateStatus(string id)
        {
            int pid = int.Parse(id.Split(',')[0]);
            var sid = int.Parse(id.Split(',')[1]);
           // bool isshow = (sid == 0 ? false : true);
            Models.clientTestimonialsModels.updateStatus(pid, sid);
            string outStr = "" + sid.ToString();
            return Json(outStr);
        }
        #endregion

        //------------------------------------
        //ct_edit PAGE
        //------------------------------------
        #region ct_edit PAGE
        public ActionResult ct_edit()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ctr_ct_edit(int? id)
        {
            int itemid = id ?? 0;
            if (itemid == 0)
                ViewBag.isAdd = true;
            else
                ViewBag.isAdd = false;

            ViewModels.ClientTestimonialViewModels item_view;
            //var item_view = new ViewModels.LanguageViewModels();
            var it = clientTestimonialsModels.getByID(itemid);
            if (it != null)
            {
                item_view = new ClientTestimonialViewModels()
                {
                    client_id = it.client_id,
                    client_name = it.client_name,
                    client_email = it.client_email,
                    client_phone = it.client_phone,
                    client_comment = it.client_comment,
                    date_modified = it.date_modified?? Udate_time.GetCurrentDateTime(),
                    enable = it.enable ?? admin_const.DEFAULT_ITEM_STATUS,
                    order_display = it.order_display ?? admin_const.DEFAULT_ORDER_DISPLAY,
                    language_id = it.language_id ?? admin_const.DEFAULT_LANGUAGE_ID
                     

                };
            }
            else
                item_view = new ClientTestimonialViewModels()
                {
                    enable = admin_const.DEFAULT_ITEM_STATUS,
                    language_id = admin_const.DEFAULT_LANGUAGE_ID,
                    order_display = admin_const.DEFAULT_ORDER_DISPLAY,
                    //listLanguage = languageModels.get_list_language()
                };

            var lst_lang = languageModels.get_list_language();
            item_view.select_list_language = new SelectList(lst_lang, "language_id", "language_desc", item_view.language_id);
            item_view.select_list_status = new List<SelectListItem>() {  
                    new SelectListItem{Selected=item_view.enable == 1?true:false, Text = "Kích hoạt", Value = "1" },
                    new SelectListItem{Selected=item_view.enable == 0?true:false, Text = "Vô hiệu hóa", Value = "0" }
            };

            return PartialView("../Shared/client_testimonial/edit_ct", item_view);
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult ctr_ct_edit(FormCollection collection)
        {
            var item_view = new ViewModels.ClientTestimonialViewModels();
            TryUpdateModel(item_view);

            if (string.IsNullOrEmpty(item_view.client_name) || string.IsNullOrEmpty(item_view.client_comment))
            {
                ViewBag.result_message = "Tên và Nội dung ý kiến khách hàng không để trống.";
                ViewBag.result = false;
                return PartialView("../Shared/client_testimonial/edit_ct", item_view);
            }
            var item = new data.entities.client_testimonials()
            {
                client_id = item_view.client_id,
                client_name = item_view.client_name,
                client_email = item_view.client_email,
                client_phone = item_view.client_phone,
                client_comment = item_view.client_comment,
                date_modified = Udate_time.GetCurrentDateTime(),
                enable = item_view.enable,
                order_display = item_view.order_display,
                language_id = item_view.language_id
            };

            int ret_id = clientTestimonialsModels.adm_client_testimonial_update(item);

            if (ret_id > 0)
            {
                ViewBag.result_message = admin_const.UPDATE_OK;
                ViewBag.redirect = "/vi/admClientTestimonial/ct_list";
                return PartialView("../Shared/control_redirect");
            }
            else
            {
                ViewBag.result_message = admin_const.UPDATE_FAIL;
                ViewBag.result = false;
                return PartialView("../Shared/client_testimonial/edit_ct", item);
            }
        }
        #endregion
    }
}